import { app, BrowserWindow, globalShortcut, Menu } from 'electron';
import { join } from 'path'


export default async function createBrowserWindow () {
  const win = new BrowserWindow({
    title: 'dist',
    width: 1440,
    height: 900,
    webPreferences: {
      preload: join(__dirname, './preload.js')
    }
  })
  Menu.setApplicationMenu(null)
  if (app.isPackaged) {
    await win.loadFile(join(__dirname, '../index.html'));
  } else {
    await win.loadURL(process.env.VITE_DEV_SERVER_URL)
    win.webContents.openDevTools()
  }
  function addShortcuts () {
    globalShortcut.register('F5', () => {
      win.webContents.reload()
    })
    globalShortcut.register('CommandOrControl+Shift+I', () => {
      win.webContents.toggleDevTools()
    })
  }
  addShortcuts()
  win.on('focus', addShortcuts)
  win.on('blur', () => {
    globalShortcut.unregisterAll()
  })
  return win
}
